
/**
 * Module dependencies.
 */

module.exports = function(db){
	var express = require('express');
	var schemas = require('./schemas/schema')();
	var routes = require('./routes')(schemas);
	var path = require('path');
	var MongoStore = require('connect-mongo')(express);
	var passport = require('./auth')(schemas);
	var dirname = __dirname;

	var app = express();

	// all environments
	app.set('port', process.env.PORT || 80);
	app.set('views', path.join(__dirname, 'views'));
	app.use(express.cookieParser());
	app.use(express.session({
		secret : 'dridanu danu danai',
		store: new MongoStore({
			mongoose_connection: db
		})
	}) );
	app.use(passport.initialize());
	app.use(passport.session());
	app.use(express.bodyParser());
	app.use(express.logger('dev'));
	app.use(express.methodOverride());
	app.use(function(req, res, next){
		req.db = db;
	    req.abs_path = dirname;
		res.set('X-Powered-By', 'TrendSettr');
		next();
	});
	app.use(app.router);
	app.use(express.static(path.join(__dirname, 'public')));

	// development only
	if ('development' == app.get('env')) {
	  app.use(express.errorHandler());
	}
	
	// Users routes
	
	app.get('/', routes.index);
	app.get('/error', routes.error);
	// list of users
	app.get('/users', routes.users);
	// show user meta if logged in
	app.get('/user', routes.user);
	
	//update all users points 
	//app.get('/updatePoints', routes.updatePoints);
	
	// create a unique user
	app.post('/user', routes.addUser);
	app.post('/login', passport.authenticate('local', {
		failureRedirect: '/error',
		successRedirect: '/user'
	}) );
	app.get('/logout', routes.logout);
	
	// topics
	// get all topics
	app.get('/topics', routes.topics);
	// get all topics by current user
	app.get('/user/topics', routes.topicsByUser);
	// create new unique topic.
	app.post('/topic', routes.addTopic);
	// delete topic and if there are trends - trends as well
	app.delete('/topic/:topicName', routes.deleteTopic);
	
	// trends
	// get all trends
	app.get('/trends', routes.trends);
	// get all trends not voted by user
	app.get('/trends/novotes', routes.notVotedTrends);
	// get all trends by current user
	app.get('/user/trends', routes.trendsByUser);
	// create new unique trend.
	app.post('/trend', routes.addTrend);
	// delete all trends by topic
	app.delete('/trends/:topicName', routes.deleteTrendsByTopic);
	
	// votes
	app.get('/trends/votes', routes.votes);
	// get votes by trend by day
	app.get('/trends/votes/all', routes.votesAll);
	
	//The votes can be in the entity or the url
	app.post('/votes/day', routes.votesByDay_reroute); 
	app.post('/vote', routes.vote);
	app.post('/downvote', routes.downVote);
	
	//This is the route for the report post. Basically users can report trends that are offensive,gibberish,etc
	//This shiz goes into the new table outlined in schema.js. I didnt quite know if it should be in it's own table or included in trends.
	//Seems different enough to necessitate a new table
	app.post('/report', routes.report);
	
	//TODO: Return the reports in the report table for the given trend. Do not return anything if the user isn't admin in schema.js
	//Assume that admin is set manually in mongodb for now
	app.get('/report', routes.getReport);
	
	//TODO: When I send over the user tokens for Google+/Facebook, cross-reference the user's friends with the emails available in our system 
	//that also use the same service. For example if the user logs in with Facebook, check to see if any of their Facebook friends are also 
	//registered in our system with Facebook. 
	//When a request is sent to this controller, report on if the user's friends voted for a trend favorably or not. Return an object like this...
	/*
	 *  {
	 *  	username : friend_name
	 *  	picture  : friend_picture
	 *      vote     : friend_vote_for_against
	 *  }
	 */
	app.post('/friend_vote', routes.friendVote);
	
	//TODO: This really isn't a todo. Just showing you what I took out
	//These can't be used anymore. Testers kept putting trends that were difficult to parse in url
	//Decided to just put the requests in the post/get headers instead of writing regex expressions over and over again
	//app.post('/vote/:trendName', routes.vote);
	//app.post('/downvote/:trendName', routes.downVote);
	//app.get('/votes/:trendName/day', routes.votesByDay); 
	
	return app;
};
